package com.diligent.cloud.sfc.factory.impl;

import com.canny.commons.model.ObjectResult;
import com.canny.commons.model.Result;
import com.canny.commons.page.IPage;
import com.canny.commons.page.Pagination;
import com.diligent.cloud.logistics.container.model.LcmBoxFillDetail;
import com.diligent.cloud.logistics.container.model.LcmBoxFillMaster;
import com.diligent.cloud.logistics.container.service.LcmBoxFillDetailFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxFillMasterFeignService;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.OrderType;
import com.diligent.cloud.sfc.model.detail.WoodenCaseDetailInfo;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.workorder.enums.WoOrderStatus;
import com.diligent.cloud.workorder.model.WoOrder;
import com.diligent.cloud.workorder.model.WoOrderCompleteDetail;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.foundation.data.model.v2.Condition;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * WoodenCaseStation class
 * 打木箱工位
 *
 * @author xf107278
 * @date 6/11/2020
 */
@Component
public class WoodenCaseStation implements StationFactory {

    private final ISfcService sfcService;
    private final WoOrderFeignService orderFeignService;
    private final LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService;
    private final LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService;
    private final WearStripsStation wearStripsStation;

    public WoodenCaseStation(ISfcService sfcService, WoOrderFeignService orderFeignService,
                             LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService,
                             LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService,
                             WearStripsStation wearStripsStation) {
        this.sfcService = sfcService;
        this.orderFeignService = orderFeignService;
        this.lcmBoxFillDetailFeignService = lcmBoxFillDetailFeignService;
        this.lcmBoxFillMasterFeignService = lcmBoxFillMasterFeignService;
        this.wearStripsStation = wearStripsStation;
    }

    /**
     * todo  还差项目匹配率 ，木箱备料进度同项目-效率问题 木箱是否属于容器管理问题
     *
     * @param order
     * @return
     */
    @Override
    public Result getOrderInfo(WoOrder order) {
        WoodenCaseDetailInfo woodenCaseDetailInfo = new WoodenCaseDetailInfo();

        WoOrderCompleteDetail orderCompleteByOrder = sfcService.getOrderCompleteByOrder(order.getId(),
                order.getWoOrder());

        if (orderCompleteByOrder != null) {
            woodenCaseDetailInfo.setGoodDigit(orderCompleteByOrder.getGoodDigit().longValue());
            woodenCaseDetailInfo.setCompleteDigit(orderCompleteByOrder.getCompleteDigit().longValue());
        } else {
            woodenCaseDetailInfo.setGoodDigit(0L);
            woodenCaseDetailInfo.setCompleteDigit(0L);
        }
        //TODO 木箱 不放容器管理？
        getSameProjectMatCode(order, woodenCaseDetailInfo);
        woodenCaseDetailInfo.setRequireDigit(order.getDeliveryDigit().intValue());
        woodenCaseDetailInfo.setBoxingStandard(order.getStandardDigit().intValue());
        woodenCaseDetailInfo.setSamePackUndoneOrder(wearStripsStation.getSameMatCodeOrder(order, "cv8",
                order.getCv8(), WoOrderStatus.schedulings.getStatus()));
        woodenCaseDetailInfo.setSamePackCompleteOrder(wearStripsStation.getSameMatCodeOrder(order, "cv8",
                order.getCv8(), WoOrderStatus.finish.getStatus()));

        return new ObjectResult<>(woodenCaseDetailInfo);
    }

    private void getSameProjectMatCode(WoOrder order, WoodenCaseDetailInfo woodenCaseDetailInfo) {
        PageAndCondition pageAndCondition = new PageAndCondition();
        pageAndCondition.setPagination(new Pagination(1L, 1000000L));
        List<Condition> conditions = new ArrayList<>();
        conditions.add(new Condition("projectCode", "eq", order.getProjectCode(), null));
        conditions.add(new Condition("orderType", "eq", OrderType.包装工单, null));

        ResponseEntity<com.diligent.cloud.primary.data.model.Result<IPage<WoOrder>>> page =
                orderFeignService.page(pageAndCondition);

        List<WoodenCaseDetailInfo.ProjectMat> projectMats = new ArrayList<>();
        for (WoOrder woOrder : page.getBody().getData().getRecords()) {
            WoodenCaseDetailInfo.ProjectMat projectMat = new WoodenCaseDetailInfo.ProjectMat();
            projectMat.setMatCode(woOrder.getMatCode());
            projectMat.setLength(Integer.valueOf(woOrder.getCv4()));
            projectMat.setOrderDigit(woOrder.getDeliveryDigit().intValue());

            //TODO 未知点计算
            projectMat.setCompleteDigit(0);
            projectMat.setBoxingDigit(getBoxingDigit(woOrder));
            projectMats.add(projectMat);
        }
        woodenCaseDetailInfo.setProjectMats(projectMats);
    }

    private Integer getBoxingDigit(WoOrder order) {
        PageAndCondition pageAndCondition = new PageAndCondition();
        List<Condition> conditions;
        pageAndCondition.setPagination(new Pagination(1L, 10000L));
        pageAndCondition.setConditions(Collections.singletonList(new Condition("refWoOrder", "eq", order, null)));
        ResponseEntity<com.diligent.cloud.primary.data.model.Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailPage =
                lcmBoxFillDetailFeignService.page(pageAndCondition);
        //查询到装卸单

        Integer digit = 0;
        for (LcmBoxFillDetail lcmBoxFillDetail : lcmBoxFillDetailPage.getBody().getData().getRecords()) {
            pageAndCondition.setPagination(new Pagination(1L, 1L));
            conditions = new ArrayList<>();
            conditions.add(new Condition("id", "eq", lcmBoxFillDetail.getMasterId(), null));
            conditions.add(new Condition("isCanceled", "eq", 0, null));
            conditions.add(new Condition("isFinished", "eq", 0, null));
            conditions.add(new Condition("isClosed", "eq", 0, null));
            pageAndCondition.setConditions(conditions);
            ResponseEntity<com.diligent.cloud.primary.data.model.Result<IPage<LcmBoxFillMaster>>> page =
                    lcmBoxFillMasterFeignService.page(pageAndCondition);
            if (page.getBody().getData().getRecords().size() != 0) {
                digit++;
            }
        }
        return digit;
    }
}
